﻿using System;
using System.Collections.Generic;
using System.Linq;
using GC_FFW.BusinessEntities.Abstractions.Interfaces.Soccerfame;
using GC_FFW.BusinessEntities.Enums.Soccerfame;
using GC_FFW.BusinessEntities.Extensions.Enums.Soccerfame;
using GC_FFW.BusinessEntities.Soccerfame;
using GC_FFW.Common.Constants.HTML;
using GC_FFW.Common.Constants.Text;
using GC_FFW.SoccerfameParser.Abstractions.Adapters;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Adapters;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Factories;
using GC_FFW.SoccerfameParser.Abstractions.Managers;
using HtmlAgilityPack;

namespace GC_FFW.SoccerfameParser.Adapters
{
	public class MatchAdapter : BaseAdapter, IMatchAdapter
	{
		#region Constants

		private const string MATCH_EVENTS_CONTAINER_ID = "showIncedents";

		#endregion

		#region Constructors

		public MatchAdapter(HtmlDocumentBaseManager htmlDocumentManager, ISoccerfameDataHelperFactory soccerfameDataHelperFactory)
			: base(htmlDocumentManager, soccerfameDataHelperFactory) { }

		#endregion

		#region IMatchAdapter Members

		public IEnumerable<IEvent> GetEvents(string url, IEnumerable<EventType> types)
		{
			if (types == null) return null;

			var htmlDocument = HtmlDocumentManager.CreateHtmlDocument(url);
			if (htmlDocument == null) return null;

			var pageMainContent = htmlDocument.GetElementbyId(MATCH_EVENTS_CONTAINER_ID);
			if (pageMainContent == null) return null;

			var result = new List<IEvent>();

			foreach (var type in types)
			{
				var events =
					(HtmlDocumentManager.FindNodesByAttributeValue(pageMainContent, TagAttributes.SRC, type.EventImageName(), true, type.GetBadValueParts())
					 ?? new List<HtmlNode>()).ToList();
				if (!events.Any()) continue;

				foreach (var eventItem in events)
				{
					var data = new Event
					{
						Type = type
					};

					var eventContainer = eventItem.ParentNode;
					if (eventContainer == null) continue;

					var num = 0;
					var time = HtmlDocumentManager.GetFullInnerTextString(eventContainer.PreviousSibling) ?? string.Empty;
					data.IsHomeTeamEvent = !(time.Contains(Symbols.APOSTROPHE)
						&& int.TryParse(time.Substring(0, time.IndexOf(Symbols.APOSTROPHE, StringComparison.Ordinal)), out num));
					if (data.IsHomeTeamEvent)
					{
						time = HtmlDocumentManager.GetFullInnerTextString(eventContainer.NextSibling) ?? string.Empty;
						if (!time.Contains(Symbols.APOSTROPHE)
							|| !int.TryParse(time.Substring(0, time.IndexOf(Symbols.APOSTROPHE, StringComparison.Ordinal)), out num))
							continue;
					}
					data.Time = num;

					var hyperlinkContainer = data.IsHomeTeamEvent ? eventContainer.PreviousSibling : eventContainer.NextSibling;
					data.Player =
						HtmlDocumentManager.GetHyperlink(hyperlinkContainer != null && hyperlinkContainer.ChildNodes != null
							? hyperlinkContainer.ChildNodes.FirstOrDefault(cn => cn.Name == TagNames.ANCHOR)
							: null)
						?? new Hyperlink
						{
							Name = HtmlDocumentManager.GetFullInnerTextString(hyperlinkContainer)
						};
					if (data.Player != null && SoccerfameDataHelperFactory.PlayerNamesService != null)
						data.Player.Name = SoccerfameDataHelperFactory.PlayerNamesService.GetValidName(data.Player.Name);

					result.Add(data);
				}
			}

			return result;
		}

		#endregion
	}
}
